/*jslint nomen: true, browser: true, plusplus: true */
/*global console, alert*/
(function (window, doc) {
	"use strict";
	var treeList = {},
		domLeafTemplate = doc.createElement("li"),
		domFolderTemplate = doc.createElement("li"),
		Node = function (_id, _value, _type) {
			var domNode;
			if (_type === "root" || _type === "folder") {
				domNode = domFolderTemplate.cloneNode(true);
				this.folderDOM = domNode.querySelector("ul");
			} else {
				domNode = domLeafTemplate.cloneNode(true);
			}
			domNode.appendChild(doc.createTextNode(_value));

			this.value = _value;
			this.id = _id;
			this.type = _type;
			this.children = [];
			this.dom = domNode;
			this.parent = null;
		},
		Tree = function (_container, treeName) {
			var rootNode = new Node(treeName, treeName, "root");
			this.container = _container;
			this.treeName = treeName;
			this.root = rootNode;
			this.nodeMap = {root: rootNode};
			this.nodeMap[treeName] = rootNode;
			this.container.appendChild(this.root.getDomElement());
			this.nodeList = {};
		},
		utree = {
			makeTree : function (_containerId, _treeName) {
				var container, tree;
				if (_containerId) {
					container = doc.getElementById(_containerId);
				} else {
					do {
						_containerId = Math.floor(Math.random() * 100);
					} while (treeList.hasOwnProperty(_containerId));
				}
				if (!container) {
					container = doc.createElement("div");
					container.id = _containerId;
				}

				tree = new Tree(container, _treeName);
				treeList[_containerId] = tree;

				return tree;
			},
			getTree : function (_treeName) {
				var i, len;
				//return the tree according to the tree name
				if (treeList.hasOwnProperty(_treeName)) {
					return treeList[_treeName].treeName === _treeName;
				}

				for (i = 0, len = treeList.length; i < len; i += 1) {
					if (treeList[i].)
				}
			}
		};

	Tree.prototype.addNode = function (_parent_id, _value, _id, _type) {
		var parentNode;
		if (_parent_id) {
			parentNode = this.getNode(_parent_id);
		} else {
			parentNode = this.root;
		}
		return this;
	};

	Tree.prototype.removeNode = function (_id) {
		//implement
		return this;
	};

	Tree.prototype.getNode = function (_id) {
		if (this.nodeMap.hasOwnProperty(_id)) {
			return this.nodeMap[_id];
		}
	};

	Node.prototype.addChild = function (_node) {
		this.children.push(_node);
		_node.parent = this;
		return this;
	};

	Node.prototype.removeChild = function (_node) {
		if (_node) {
			this.removeChildById(_node.id);
		}
		return this;
	};

	Node.prototype.removeChildById = function (_id) {
		var i;
		for (i = children.length; i--;) {
			if (children[i].id === _id) {
				children.splice(i, 1);
			}
		}
		return this;
	};

	Node.prototype.setParent = function (_parent) {
		this.parent = _parent;
		_parent.addChild(_node);		
		return this;
	};

	Node.prototype.getDomElement = function () {
		return this.dom;
	}

	window.utree = utree;

}(window, document));